package com.atguigu.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.hosp.repository.DepartmentRepository;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.vo.hosp.DepartmentQueryVo;
import com.atguigu.yygh.vo.hosp.DepartmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @FileName: DepartmentServiceImpl
 * @Author Steven
 * @Date: 2021/3/18
 */
@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    DepartmentRepository departmentRepository;

    @Override
    public void save(Map<String, Object> paramMap) {
        Department department = JSONObject.parseObject(JSONObject.toJSONString(paramMap), Department.class);
        Department targetDepartment = departmentRepository.getDepartmentByHoscodeAndDepcode(department.getHoscode(), department.getDepcode());
        if(StringUtils.isEmpty(targetDepartment)){
            //BeanUtils.copyProperties(department,targetDepartment);
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
        }else {
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(targetDepartment);
        }
    }

    @Override
    public Page<Department> selectPage(Integer current, Integer limit, DepartmentQueryVo departmentQueryVo) {
      Sort sort=Sort.by(Sort.Direction.DESC,"createTime");
      Pageable pageable = PageRequest.of(current-1, limit, sort);
      Department department=new Department();
      BeanUtils.copyProperties(departmentQueryVo,department);
      department.setIsDeleted(0);
      ExampleMatcher exampleMatcher=ExampleMatcher.matching()
              .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
              .withIgnoreCase(true);
      //创建匹配实例
      Example<Department> example=Example.of(department,exampleMatcher);
        Page<Department> page = departmentRepository.findAll(example, pageable);
        return page;

    }

    @Override
    public void remove(String hosCode, String depCode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hosCode, depCode);
        if(!StringUtils.isEmpty(department)){
            departmentRepository.deleteById(department.getId());
        }
    }

    @Override
    public List<DepartmentVo> findDeptTree(String hosCode) {
        //创建list集合封装结果集
        List<DepartmentVo> result =new ArrayList<>();

        //根据医院编号查询所有科室信息
        Department departmentQuery=new Department();
        departmentQuery.setHoscode(hosCode);
        ExampleMatcher matcher=ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.STARTING)
                .withIgnoreCase(true);
        Example<Department> example=Example.of(departmentQuery,matcher);
        //所有列表的科室信息
        List<Department> departmentList = departmentRepository.findAll(example);

        //根据大科室编号 bigcode 分组获取每个大科室下的小科室
        Map<String, List<Department>> departmentMap = departmentList.stream().
                collect(Collectors.groupingBy(Department::getBigcode));
        //遍历map集合
        for(Map.Entry<String,List<Department>> entry:departmentMap.entrySet()){
            //大科室编号
            String bigCode = entry.getKey();
            //大科室对应全部的数据
            List<Department> departments = entry.getValue();

            //封装大科室
            DepartmentVo departmentVo=new DepartmentVo();
            departmentVo.setDepcode(bigCode);
            departmentVo.setDepname(departments.get(0).getBigname());

            //封装小科室
            List<DepartmentVo> child=new ArrayList<>();
            for(Department department:departments){
                DepartmentVo departmentVoChild=new DepartmentVo();
                departmentVoChild.setDepname(department.getDepname());
                departmentVoChild.setDepcode(department.getDepcode());
                child.add(departmentVoChild);
            }
            departmentVo.setChildren(child);
            result.add(departmentVo);
        }
        return result;
    }

    @Override
    public String getDepName(String hosCode, String deptCode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hosCode, deptCode);
        if(null!=department){
            return department.getDepname();
        }
        return null;
    }

    @Override
    public Department getDepartment(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        return department;
    }
}
